/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.achartengine.chartdemo.demo.chart;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import cc.telecomdigital.tdstock.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class XYChartBuilder extends Activity
{
	public static final String		TYPE		= "type";

	private XYMultipleSeriesDataset	mDataset	= new XYMultipleSeriesDataset();

	private XYMultipleSeriesRenderer	mRenderer	= new XYMultipleSeriesRenderer();

	private XYSeries				mCurrentSeries;

	private XYSeriesRenderer			mCurrentRenderer;

	private String					mDateFormat;

	private Button					mNewSeries;

	private Button					mAdd;

	private EditText				mX;

	private EditText				mY;

	private GraphicalView			mChartView;

	@Override
	protected void onRestoreInstanceState(Bundle savedState)
	{
		super.onRestoreInstanceState(savedState);
		mDataset = (XYMultipleSeriesDataset) savedState
				.getSerializable("dataset");
		mRenderer = (XYMultipleSeriesRenderer) savedState
				.getSerializable("renderer");
		mCurrentSeries = (XYSeries) savedState
				.getSerializable("current_series");
		mCurrentRenderer = (XYSeriesRenderer) savedState
				.getSerializable("current_renderer");
		mDateFormat = savedState.getString("date_format");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		outState.putSerializable("dataset", mDataset);
		outState.putSerializable("renderer", mRenderer);
		outState.putSerializable("current_series", mCurrentSeries);
		outState.putSerializable("current_renderer", mCurrentRenderer);
		outState.putString("date_format", mDateFormat);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xy_chart);
		mX = (EditText) findViewById(R.id.xValue);
		mY = (EditText) findViewById(R.id.yValue);

		mAdd = (Button) findViewById(R.id.add);
		mNewSeries = (Button) findViewById(R.id.new_series);
		mNewSeries.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				String seriesTitle = "Series "
						+ (mDataset.getSeriesCount() + 1);
				XYSeries series = new XYSeries(seriesTitle);
				mDataset.addSeries(series);
				mCurrentSeries = series;
				XYSeriesRenderer renderer = new XYSeriesRenderer();
				mRenderer.addSeriesRenderer(renderer);
				mCurrentRenderer = renderer;
				setSeriesEnabled(true);
			}
		});

		mAdd.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				double x = 0;
				double y = 0;
				try
				{
					x = Double.parseDouble(mX.getText().toString());
				}
				catch (NumberFormatException e)
				{
					// TODO
					mX.requestFocus();
					return;
				}
				try
				{
					y = Double.parseDouble(mY.getText().toString());
				}
				catch (NumberFormatException e)
				{
					// TODO
					mY.requestFocus();
					return;
				}
				mCurrentSeries.add(x, y);
				mX.setText("");
				mY.setText("");
				mX.requestFocus();
				if (mChartView != null)
				{
					mChartView.repaint();
				}
			}
		});
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		if (mChartView == null)
		{
			LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
			mChartView = ChartFactory.getLineChartView(this, mDataset,
					mRenderer);
			layout.addView(mChartView, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			boolean enabled = mDataset.getSeriesCount() > 0;
			setSeriesEnabled(enabled);
		}
		else
		{
			mChartView.repaint();
		}
	}

	private void setSeriesEnabled(boolean enabled)
	{
		mX.setEnabled(enabled);
		mY.setEnabled(enabled);
		mAdd.setEnabled(enabled);
	}
}
